//
//  ONEChatAddViewController.m
//  ONEChat
//
//  Created by 楼楚 on 15/12/23.
//  Copyright © 2015年 Chu Lou. All rights reserved.
//

#import "ONEChatAddViewController.h"
#import "ONEChatContactViewController.h"
@interface ONEChatAddViewController ()<UITextFieldDelegate>
@end

@implementation ONEChatAddViewController
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"adding");
    NSString *user = textField.text;
    NSLog(@"%@",user);
        if ([user isEqualToString:[ONEChatUserInfo sharedONEChatUserInfo].user]) {
        [self showAlert:@"不能添加自己"];
        return YES;
    }
     NSString *jidStr = [NSString stringWithFormat:@"%@@%@",user,domain];
    XMPPJID *friendjid = [XMPPJID jidWithString:jidStr];
    if ([[ONEChatXMPPTool sharedONEChatXMPPTool].rosterStorge userExistsWithJID:friendjid xmppStream:[ONEChatXMPPTool sharedONEChatXMPPTool].xmppStream]) {
        [self showAlert:@"好友已经存在"];
        return YES;
    }
    
    [[ONEChatXMPPTool sharedONEChatXMPPTool].roster subscribePresenceToUser:friendjid];
    return YES;
    
}

- (void) showAlert:(NSString *)msg
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:@"谢谢" otherButtonTitles:nil, nil];
    [alert show];
}

@end
